<?php
class OrderController extends Core\Api{
    public function createAction(){
        $user_id = $this->session->user['id'];
        if(!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }

        $jsondata = $this->post('jsondata');
        $data = json_decode($jsondata,true);
        /*
        $data = array(
            0=>array(
                'product_id' => 1,
                'quantity' => 1,
                'product_discount_id' => 1,
            )
        );
         */
        //echo json_encode($data);
        if(!is_array($data)) {
            //数据类型错误
            $ret = $this->error('params_error');
            return $this->json($ret);
        }
        foreach($data as $k=> &$v) {
            if (
                !isset($v['product_id'])
                || !isset($v['quantity'])
            ) {
                unset($v);
            }
        }
        //此时已经保证items数据以确保完整
        $res = \Business\UserModel::createOrder($user_id,$data);
        if ($res) {
            $ret = $this->ok($res);
        } else {
            $ret = $this->error('订单创建失败');
        }
        return $this->json($ret);
    }
    public function pay_typeAction(){
        $user_id = $this->session->user['id'];
        if(!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
        $order_id = $this->post('order_id');
        $pay_type = $this->post('pay_type','ecoin');
        if(!$order_id || !$pay_type) {
            $ret = $this->error('params_error');
            return $this->json($ret);
        }
        $data = array();
        $data['pay_type'] = $pay_type;
        $res = \Business\UserModel::setOrder($user_id,$order_id,$data);
        if ($res) {
            $ret = $this->ok();
        } else {
            $ret = $this->error('设置支付方失败');
        }
        return $this->json($ret);
    }
    //支付
    public function payAction() {
        $user_id = $this->session->user['id'];
        if(!$user_id) {
            $ret = $this->error('user_not_login');
            return $this->json($ret);
        }
        $out_trade_no = $this->post('out_trade_no');
        $total_fee = $this->post('total_fee');
        $remark = $this->post('remark','');
        $total_point = $this->post('total_point',0);

        if(!$out_trade_no || !$total_fee) {
            $ret = $this->error('params_error');
            return $this->json($ret);
        }
        $order_id = $out_trade_no;
        $order = \Business\UserModel::getOrder($user_id,$order_id);
        if (!$order) {
            $ret = $this->error('订单不存在');
            return $this->json($ret);
        }
        if ($order['pay_status'] !== 1 ) {
            $ret = $this->error('该订单已经支付过');
            return $this->json($ret);
        }
        $total_point_fee = \Business\SystemModel::pointTofee($total_point);
        if ($order['pay_need'] !== $total_fee + $total_point_fee) {
            $ret = $this->error('金额不匹配');
            return $this->json($ret);
        }
        \DB::beginTransaction();
        $res = \Business\UserModel::outlay($user_id,$total_point_fee,'point',$order_id,$remark);
        if (!$res) {
            $ret = $this->error('积分不足');
            return $this->json($ret);
        }
        $res = \Business\UserModel::outlay($user_id,$total_fee,'ecoin',$order_id,$remark);
        if (!$res) {
            $ret = $this->error('金额不足');
            return $this->json($ret);
        }
        $s = \Business\UserModel::setOrder($user_id,$order_id,array('pay_status'=>1));
        if(!$s ){
            \DB::rollback();
        }
        \DB::commit();
        $ret = $this->ok();
        return $this->json($ret);
    }
}
